package Hxw3DEngine.core.Resource
{
	import ao3s2.Flash3dLib.CModule;
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author lighter
	 */
	public class ResourceSkeleton implements IResourceAtom
	{
		
		private var _mtx_type:uint;
		private var _joints:Vector.<String>;
		private var _jointsNameLookup:Dictionary;
		private var _parents:int;
		private var _bind_mtx:int;
		
		public function ResourceSkeleton(vo:Object = null)
		{
			if (vo != null) {
				createFromVO(vo);
			}
		}
		
		/* INTERFACE Hxw3DEngine.core.Resource.IResourceAtom */
		
		public function createFromVO(vo:Object):Boolean
		{
			_mtx_type = vo.mtx_type;
			
			_joints = new Vector.<String>();
			_jointsNameLookup = new Dictionary();
			for each (var name:String in vo.joints)
			{
				_jointsNameLookup[name] = _joints.length;
				_joints.push(name);
			}
			_parents = CModule.malloc(vo.parents.length);
			_bind_mtx = CModule.malloc(vo.bind_mtx.length);
			CModule.writeBytes(_parents, vo.parents.length, vo.parents);
			CModule.writeBytes(_bind_mtx, vo.bind_mtx.length, vo.bind_mtx);
			
			return true;
		}
		
		public function dispose():void
		{
			// 执行当前类实例的清理操作
			CModule.free(_parents);
			CModule.free(_bind_mtx);
			_parents = 0;
			_bind_mtx = 0;
			_joints = null;
			_jointsNameLookup = null;
			// 调用父级操作
			super.dispose();
		}
		
		public function jointName(index:uint):String
		{
			if (index < _joints.length)
				return _joints[index];
			else
				return null;
		}
		
		public function jointIndexFromName(jointName:String):int
		{
			if (jointName in _jointsNameLookup)
			{
				return _jointsNameLookup[jointName];
			}
			return -1;
		}
		
		public function get parents():int
		{
			return _parents;
		}
		
		public function get bind_mtx():int
		{
			return _bind_mtx;
		}
		
		public function get numberOfJoints():uint
		{
			return _joints.length;
		}
	}

}